package com.yc.bank.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yc.bank.entity.Account;
import com.yc.bank.entity.Oprecord;
import com.yc.bank.mapper.AccountMapper;
import com.yc.bank.mapper.OprecordMapper;
import com.yc.bank.service.IAccountService;
import com.yc.bank.service.IOprecordService;
import com.yc.bank.util.MyBatisHelper;
import org.apache.ibatis.javassist.compiler.ast.Variable;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;

@Service
@Transactional
public class AccountBiz {

    @Resource
    IOprecordService iOprecordService;

    //存款
    public void deposit(String id, double money, String remark, IAccountService accountService, IOprecordService oprecordService) {
//        SqlSession session = MyBatisHelper.getSession();
//        try {
        Oprecord oprecord = new Oprecord();
        oprecord.setAid(Integer.parseInt(id));
        oprecord.setRemark(remark);
        oprecord.setOpmoney(BigDecimal.valueOf(money));
        oprecord.setOptime(LocalDateTime.now());

//
//        LambdaUpdateWrapper<Oprecord> lqw1 = new LambdaUpdateWrapper<>();
//        lqw1.set(Oprecord::getOpmoney, money);
//        lqw1.set(Oprecord::getRemark, remark);
//        lqw1.eq(Oprecord::getId, id);
        oprecordService.save(oprecord);

        Account byId = accountService.getById(id);

        LambdaUpdateWrapper<Account> lqw2 = new LambdaUpdateWrapper<>();
        lqw2.set(Account::getBalance, BigDecimal.valueOf(money).add(byId.getBalance())).eq(Account::getId, id);
        accountService.update(lqw2);


//            AccountMapper accountMapper = session.getMapper(AccountMapper.class);
//            OprecordMapper oprecordMapper = session.getMapper(OprecordMapper.class);
//            accountMapper.updateBalance(id, money);
//            Oprecord oprecord = new Oprecord();
//            oprecord.setAid(Integer.valueOf(id));
//            oprecord.setOpmoney(BigDecimal.valueOf(money));
//            oprecord.setRemark(remark);
//            oprecordMapper.insert(oprecord);
//            session.commit();
//        } catch (RuntimeException e) {
//            e.printStackTrace();
//            session.rollback();
//        } finally {
//            session.close();
//        }

    }

    //取款
    public void withdraw(String id, double money, String remark,IAccountService accountService,IOprecordService iOprecordService) throws BizExcepion {
//        try {
//
//        }
    }

    //转账
    //noRollbackFor  不会回滚异常 ==》类型 ：运行时异常
    @Transactional(rollbackFor = {BizExcepion.class})
    public void transform(String id1, String id2, double money, String remark) throws BizExcepion {

//        deposit(id1, money, remark,);
//        withdraw(id2, money, remark);
    }
}
